//
//  CommunityViewController.m
//  PTravel
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 李涵. All rights reserved.
//

#import "CommunityViewController.h"


@interface CommunityViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (retain, nonatomic)UICollectionView *myCollection;
@property (retain, nonatomic)NSMutableArray *continentArr;//洲组
@property (retain, nonatomic)NSMutableArray *nameArr;//名租
@property (retain, nonatomic)NSMutableArray *nArr;//名租
@property (retain, nonatomic)NSNumber *answerN;
@property (retain, nonatomic)NSNumber *companyN;
@property (retain, nonatomic)MBProgressHUD *hub;

@end

@implementation CommunityViewController

- (void)dealloc
{
    [_myCollection release];
    [_continentArr release];
    [_nameArr release];
    [_nArr release];
    [_answerN release];
    [_companyN release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    //夜间模式判断
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"night" object:nil];
}

- (void)changeColor:(NSNotificationCenter *)notification
{
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:TEXTCOLOR};
    self.navigationController.navigationBar.barTintColor = BARCOLOR;
    self.myCollection.backgroundColor = CELLCOLOR;
    [self.myCollection reloadData];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.barTintColor = BARCOLOR;
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:TEXTCOLOR};
    self.automaticallyAdjustsScrollViewInsets = YES;
    
   
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    
    self.myCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, BOUNDS.size.width, BOUNDS.size.height) collectionViewLayout:flowlayout];
    self.myCollection.delegate = self;
    self.myCollection.dataSource = self;
    self.myCollection.backgroundColor = CELLCOLOR;
    [self.view addSubview:self.myCollection];
    [_myCollection release];
    [flowlayout release];
    //注册
    [self.myCollection registerClass:[CommunityCollectionViewCell class] forCellWithReuseIdentifier:@"CommunityCollectionViewCell"];
    [self.myCollection registerClass:[CommunityCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CommunityCollectionReusableView"];
    [self.myCollection registerClass:[Head class] forCellWithReuseIdentifier:@"head"];
    
    [self getData];

    self.navigationItem.title = @"四海兄弟";
    
}

#pragma mark === 请求数据

- (void)getData
{
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"Waiting···";
    self.continentArr = [NSMutableArray array];
    self.nArr = [NSMutableArray array];
    [AppTools getAFN:@"http://open.qyer.com/qyer/bbs/entry?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88255639279708&lon=121.5396443687879&oauth_token=7a1da33fff703bde846a4d6980671677&page=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C2&track_deviceid=94E2CF74-5FF7-49B9-8D69-620BF45F65A9&track_os=ios%25209.1&track_user_id=7017902&v=1" block:^(id result) {
        NSMutableDictionary *bigDic = result;
        NSMutableDictionary *smallDic = [bigDic objectForKey:@"data"];
        
        NSMutableDictionary *countDic = [smallDic objectForKey:@"counts"];
        self.answerN = [countDic objectForKey:@"ask"];
        self.companyN = [countDic objectForKey:@"company"];
        
        NSMutableArray *listArr = [smallDic objectForKey:@"forum_list"];
        for (NSMutableDictionary *dic in listArr) {
            
            
            self.nameArr = [NSMutableArray array];
            ListGroup *listgroup = [[ListGroup alloc] init];
            [listgroup setValuesForKeysWithDictionary:dic];
            [self.continentArr addObject:listgroup];
            NSMutableArray *groupArr = [dic objectForKey:@"group"];
            
            self.nameArr = [NSMutableArray array];
            
            for (NSMutableDictionary *dic1 in groupArr) {
                Community *community = [[Community alloc] init];
                [community setValuesForKeysWithDictionary:dic1];
                [self.nameArr addObject:community];
            }
            [self.nArr addObject:self.nameArr];
        }
        [self.myCollection reloadData];
        [self.hub hide:YES];
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
//    return self.continentArr.count+1;
    return self.continentArr.count;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//    if (section == 0) {
//        return  1;
//    }else{
    
//        return [[self.nArr objectAtIndex:section-1] count];
    return [[self.nArr objectAtIndex:section] count];
//    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.section == 0) {
//        Head *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"head" forIndexPath:indexPath];
//        cell.answerNum.text = [NSString stringWithFormat:@"%@个问题在得到解决", self.answerN];
//        cell.companyNum.text = [NSString stringWithFormat:@"%@个网友在此结伴", self.companyN];
//        cell.answerNum.numberOfLines = 0;
//        cell.companyNum.numberOfLines = 0;
//        cell.answerNum.textColor = TEXTCOLOR;
//        cell.companyNum.textColor = TEXTCOLOR;
//        cell.backgroundColor = CELLCOLOR;
//        return cell;
//    }else{
    
        static NSString *cellIdentifier = @"CommunityCollectionViewCell";
        CommunityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
//        cell.community = [self.nArr[indexPath.section-1] objectAtIndex:indexPath.item];
        cell.community = [self.nArr[indexPath.section] objectAtIndex:indexPath.item];
        cell.backgroundColor = CELLCOLOR;
        return cell;
//    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    CommunityCollectionReusableView *reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CommunityCollectionReusableView" forIndexPath:indexPath];
//    if (indexPath.section == 0) {
//        reusable.label.frame = CGRectMake(0, 0, 0, 0);
//    }else{
//        reusable.listGroup = [self.continentArr objectAtIndex:indexPath.section-1];
    reusable.listGroup = [self.continentArr objectAtIndex:indexPath.section];
    
//    }
    return reusable;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.section == 0) {
//        return CGSizeMake(BOUNDS.size.width, 60);
//    }else{
    
        return CGSizeMake(BOUNDS.size.width/2-15, 55);
//    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
//    if (section == 0) {
//        return CGSizeMake(0, 0);
//    }else{
//        
        return CGSizeMake(BOUNDS.size.width, 30);
//    }
}

#pragma mark === 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CommunityDetailViewController *detail = [[CommunityDetailViewController alloc] init];
    [self.navigationController pushViewController:detail animated:YES];
    Community *community = [self.nArr[indexPath.section] objectAtIndex:indexPath.item];
    detail.forum_id = community.id;
    detail.name = community.name;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
